//
//  RwdTableViewCell.swift
//  Weibo_rwd
//
//  Created by 任文迪 on 16/7/13.
//  Copyright © 2016年 spaceX. All rights reserved.
//

import UIKit
import SnapKit

class RwdTableViewCell: UITableViewCell {
    //懒加载三个view
    private lazy var topView: RwdUserInfosView = RwdUserInfosView()
    private lazy var toolBar: RwdBottomToolBar = RwdBottomToolBar()
    private lazy var retweetView: RwdRetweetView = RwdRetweetView()
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        SetUpUI()
    }
    var toolbarTopConstraint: Constraint?
    
    //判断是否有转发内容 如果有就显示retweetView 如果没有就隐藏 并更新约束
    
    //模型赋值
    var statusViewModel:RwdStatusViewModel? {
        
        didSet{
            //上部视图的模型赋值
            topView.statusViewModel = statusViewModel
            //下部视图的模型赋值
            toolBar.statusModel = statusViewModel
            //先卸载约束
            self.toolbarTopConstraint?.uninstall()
            //判断是否存在转发内容
            if statusViewModel?.status?.retweeted_status != nil {
                //转发视图的模型赋值
                retweetView.statusModel = statusViewModel
                retweetView.hidden = false
                
                
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolbarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                    
                })
                
            }else {
                retweetView.hidden = true
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolbarTopConstraint = make.top.equalTo(topView.snp_bottom).constraint
                })
                
                
            }
        }
        
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //在cell的contentView添加子控件
    private func SetUpUI() {
        contentView.backgroundColor = UIColor.lightGrayColor()
        //添加子视图
        contentView.addSubview(topView)
        contentView.addSubview(toolBar)
        contentView.addSubview(retweetView)
        //设置约束
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(margin-5)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        topView.backgroundColor = UIColor.whiteColor()
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        retweetView.backgroundColor = UIColor.whiteColor()
        toolBar.snp_makeConstraints { (make) -> Void in
            self.toolbarTopConstraint =  make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
        toolBar.backgroundColor = UIColor.whiteColor()
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar.snp_bottom).offset(5)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.top.equalTo(self)
        }
    }
    
    
    

}
